package cn.edu.jxau.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.edu.jxau.domain.notice;
import cn.itcast.jdbc.TxQueryRunner;

public class NoticeDao {
   
	private QueryRunner qr = new TxQueryRunner();

	/**
	 * 添加推送的公告进数据库
	 * 
	 * @param notice
	 */
	public void addNotice(notice notice) {
		String sql = "insert into notice values(?,?,?,?)";
		Object[] params = {notice.getNoticeId(),notice.getNotice(),notice.getName(),notice.getUpdatetime()};
		try {
			qr.update(sql, params);
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
	/**
	 * 查询所有公告  返回值：notice对象
	 */
	public List<notice> queryNotice() {
		String sql = "select * from notice ";

		try {
			return  qr.query(sql,new BeanListHandler<notice>(notice.class));
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
	
	/**
	 * 根据ID删除相应的公告
	 */
	public void delByID(String noticeid){
		String sql="delete from notice where noticeId=?";
		try {
			qr.update(sql, noticeid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 根据公告ID修改公告内容
	 */
	public void updateNotice(String NoticeID,String content) {
		String sql = "update notice set notice=? where noticeId=?";
		Object[] param = { content, NoticeID };
		try {
			qr.update(sql, param);
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
	
}
